package cn.ffcs.uec.selfstaff.component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Div;
import org.zkoss.zul.Messagebox;

import cn.ffcs.uec.selfstaff.component.bean.UecSelfStaffInfoEditBean;
import cn.ffcs.uec.selfstaff.constant.UecSelfStaffConstants;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecSelfStaffEditExt extends Div implements IdSpace {

	private static final long serialVersionUID = -2212731640371254189L;

	/**
	 * 对应页面上的bean
	 */
	private UecSelfStaffInfoEditBean bean = new UecSelfStaffInfoEditBean();
	/**
	 * 人员基本信息对象
	 */
	private UecStaffInfo uecStaffInfo;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecSelfStaffEditExt() throws Exception {
		Executions.createComponents(UecSelfStaffConstants.UEC_STAFF_EDIT_EXT_ZUL, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');
		this.bindCombobox();
		this.addForward(UecSelfStaffConstants.ON_UEC_SELF_STAFF_QUERY, this,
				UecSelfStaffConstants.ON_UEC_STAFF_INFO_QUERY_RESPONSE);
	}

	/**
	 * 初始化
	 * 
	 * @throws Exception
	 */
	public void onCreate() throws Exception {

	}

	public void onUecSelfStaffInfoQueryResponse(ForwardEvent event) {
		uecStaffInfo = (UecStaffInfo) event.getOrigin().getData();
		PubUtil.fillBeanFromPo(uecStaffInfo, bean);

	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */

	public void onSave() throws Exception {
		PubUtil.fillPoFromBean(bean, uecStaffInfo);
		uecStaffInfo.updateOnly();
		Messagebox.show("保存成功！");
	}

	/**
	 * 取消
	 * 
	 * @throws Exception
	 */

	public void onQuery() throws Exception {
		PubUtil.fillBeanFromPo(uecStaffInfo, bean);
	}

	/**
	 * 员工确认
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		onSave();
	}

	/**
	 * 绑定bean 集团数据翻译展示,并向其他组件跑出事件
	 * 
	 * @throws Exception
	 */
	public void bindCombobox() throws Exception {
		// 绑定性别sex:
		List<NodeVo> nodeVoSexList = new ArrayList<NodeVo>();
		String[][] sex = { { "1", "男" }, { "2", "女" }, { "3", "未知" } };
		for (int i = 0; i < sex.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(sex[i][j]);
				nodeVo.setName(sex[i][j + 1]);
			}
			nodeVoSexList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getSex(), nodeVoSexList);

		// 绑定民族nationality
		List<NodeVo> nodeVoNationalityList = new ArrayList<NodeVo>();
		String[][] nationality = { { "1001A11000000000063W", "汉族" }, { "1001A110000000000647", "侗族" },
				{ "1001A11000000000063Y", "回族" }, { "1001A110000000000646", "满族" }, { "1001A11000000000063X", "蒙古族" } };
		for (int i = 0; i < nationality.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(nationality[i][j]);
				nodeVo.setName(nationality[i][j + 1]);
			}
			nodeVoNationalityList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getNationality(), nodeVoNationalityList);

		// 婚姻状况marital
		List<NodeVo> nodeVoMaritalList = new ArrayList<NodeVo>();
		String[][] marital = { { "1001A1100000000005XQ", "未婚" }, { "1001A1100000000005XR", "已婚" },
				{ "1001A1100000000005XS", "再婚" }, { "1001A1100000000005XT", "离异" }, { "1001A1100000000005XU", "丧偶" } };
		for (int i = 0; i < marital.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(marital[i][j]);
				nodeVo.setName(marital[i][j + 1]);
			}
			nodeVoMaritalList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getMarital(), nodeVoMaritalList);

		// 健康状况
		List<NodeVo> nodeVoHealthList = new ArrayList<NodeVo>();
		String[][] health = { { "1001A1100000000005YD", "健康" }, { "1001A1100000000005YE", "一般" },
				{ "1001A1100000000005YF", "有慢性病" }, { "1001A1100000000005YG", "有生理缺陷" },
				{ "1001A1100000000005YH", "残疾" }, { "1001A1100000000005YI", "较差" } };
		for (int i = 0; i < health.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(health[i][j]);
				nodeVo.setName(health[i][j + 1]);
			}
			nodeVoHealthList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getHealth(), nodeVoHealthList);

		// 政治面貌
		List<NodeVo> nodeVoPolityList = new ArrayList<NodeVo>();
		String[][] polity = { { "1001A1100000000006D6", "中国共产党党员" }, { "1001A1100000000006D7", "中国共产党预备党员" },
				{ "1001A1100000000006D8", "中国共产主义青年团团员" }, { "1001A1100000000006DI", "群众" } };
		for (int i = 0; i < polity.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(polity[i][j]);
				nodeVo.setName(polity[i][j + 1]);
			}
			nodeVoPolityList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPolity(), nodeVoPolityList);

		// 户口性质characterrpr
		List<NodeVo> nodeVoCharacterrprList = new ArrayList<NodeVo>();
		String[][] characterrpr = { { "1001A1100000000005XM", "城镇" }, { "1001A1100000000005XN", "农村" } };
		for (int i = 0; i < characterrpr.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(characterrpr[i][j]);
				nodeVo.setName(characterrpr[i][j + 1]);
			}
			nodeVoCharacterrprList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getCharacterrpr(), nodeVoCharacterrprList);

	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getJoinpolitydate().getValue())) {
			// 入党(团)日期
			uecStaffInfo.setJoinpolitydate(formatter.format(bean.getJoinpolitydate().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getBirthdate().getValue())) {
			// 出生日期
			uecStaffInfo.setBirthdate(formatter.format(bean.getBirthdate().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getDieDate().getValue())) {
			// 身故日期
			uecStaffInfo.setDieDate(formatter.format(bean.getDieDate().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getJoinworkdate().getValue())) {
			// 参加工作日期
			uecStaffInfo.setJoinworkdate(formatter.format(bean.getJoinworkdate().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getRetiredate().getValue())) {
			// 离退休日期
			uecStaffInfo.setRetiredate(formatter.format(bean.getRetiredate().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef15().getValue())) {
			// 预计退休日期
			uecStaffInfo.setGlbdef15(formatter.format(bean.getGlbdef15().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef3().getValue())) {
			// 进入电信（邮电）系统日期
			uecStaffInfo.setGlbdef3(formatter.format(bean.getGlbdef3().getValue()));
		}
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 出生日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecStaffInfo.getBirthdate()) {
			bean.getBirthdate().setValue(sdf.parse(uecStaffInfo.getBirthdate()));
		}
		// 入党(团)日期
		if (!StrUtil.isNullOrEmpty(uecStaffInfo.getJoinpolitydate())) {
			bean.getJoinpolitydate().setValue(sdf.parse(uecStaffInfo.getJoinpolitydate()));
		}
		// 身故日期
		if (!StrUtil.isNullOrEmpty(uecStaffInfo.getDieDate())) {
			bean.getDieDate().setValue(sdf.parse(uecStaffInfo.getDieDate()));
		}
		// 参加工作日期
		if (!StrUtil.isNullOrEmpty(uecStaffInfo.getJoinworkdate())) {
			bean.getJoinworkdate().setValue(sdf.parse(uecStaffInfo.getJoinworkdate()));
		}

	}

}
